<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="患者姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付单号" prop="payOrderNo">
        <el-input
          v-model="queryParams.payOrderNo"
          placeholder="请输入支付单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.yy_pay_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dataCenter:payment:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dataCenter:payment:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dataCenter:payment:remove']"
          >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dataCenter:payment:export']"
          >导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="paymentList"
      @selection-change="handleSelectionChange"
      show-summary
      :summary-method="getSummaries"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="患者档案id" align="center" prop="patientId" /> -->

      <el-table-column
        label="支付单号"
        align="center"
        prop="payOrderNo"
        fixed
        width="210"
      />
      <el-table-column
        label="患者姓名"
        align="center"
        prop="patientName"
        fixed
        width="80"
      />
      <el-table-column
        label="门诊号"
        align="center"
        prop="outpatientNo"
        width="120"
      />
      <el-table-column label="总金额" align="center" prop="amount" />
      <el-table-column label="应收金额" align="center" prop="totalAmount" />
      <el-table-column label="收银人姓名" align="center" prop="checkerName" />
      <el-table-column label="实收金额" align="center" prop="actualPayment" />

      <el-table-column
        label="支付订单描述"
        align="center"
        prop="payOrderName"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        label="支付失效时间"
        align="center"
        prop="paymentExpirationTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.paymentExpirationTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="支付方式" align="center" prop="paymentMethod" /> -->
      <el-table-column
        label="支付方式"
        align="center"
        prop="paymentMethodName"
      />
      <!-- <el-table-column label="支付状态" align="center" prop="paymentStatus" /> -->
      <el-table-column
        label="支付状态"
        align="center"
        prop="paymentStatusStr"
      />
      <el-table-column
        label="支付时间"
        align="center"
        prop="paymentTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="医保支付类型 "
        align="center"
        prop="medicalInsuranceType"
      /> -->
      <!-- <el-table-column label="收费次数" align="center" prop="numberCharges" />  -->
      <el-table-column
        label="支付流水号"
        align="center"
        prop="paymentTransactionNo"
      />
      <el-table-column label="支付类型" align="center" prop="paymentType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_pay_types"
            :value="scope.row.paymentType"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="结算方式" align="center" prop="settlementMethod">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_settle_accounts"
            :value="scope.row.settlementMethod"
          />
        </template>
      </el-table-column> -->
      <el-table-column
        label="结算方式"
        align="center"
        prop="settlementMethodName"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_pay_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="退款人id" align="center" prop="refundCheckerId" /> -->
      <el-table-column
        label="退款人姓名"
        align="center"
        prop="refundCheckerName"
      />
      <el-table-column
        label="退款时间"
        align="center"
        prop="refundTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="退款流水号"
        align="center"
        prop="refundTransactionNo"
      />
      <el-table-column
        label="医保退款结算id"
        align="center"
        prop="refundSetlId"
      />
      <el-table-column label="个人账户支付金额" align="center" prop="payCost" />
      <el-table-column label="统筹金额" align="center" prop="pubCost" />
      <el-table-column label="现金支付金额" align="center" prop="ownCost" />
      <el-table-column label="医保支付订单号" align="center" prop="payOrdId" />
      <!-- <el-table-column label="医保结算id" align="center" prop="setlId" /> -->
      <!-- <el-table-column label="医保登记id" align="center" prop="mdtrtId" /> -->
      <el-table-column label="患者医保编号" align="center" prop="psnNo" />
      <!-- <el-table-column label="医保结算参数" align="center" prop="hsPayInfo" /> -->
      <!-- <el-table-column
        label="其他支付参数"
        align="center"
        prop="otherPaymentParameters"
      /> -->
      <el-table-column label="参保地区划" align="center" prop="insuplcAdmdvs" />
      <el-table-column label="险种类型" align="center" prop="insutype" />
      <el-table-column label="来源" align="center" prop="source">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_source_types"
            :value="scope.row.source"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dataCenter:payment:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dataCenter:payment:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改云HIS-支付汇总对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="患者姓名" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入患者姓名" />
        </el-form-item>
        <el-form-item label="实际支付金额" prop="actualPayment">
          <el-input
            v-model="form.actualPayment"
            placeholder="请输入实际支付金额"
          />
        </el-form-item>
        <el-form-item label="总金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="收银人姓名" prop="checkerName">
          <el-input v-model="form.checkerName" placeholder="请输入收银人姓名" />
        </el-form-item>
        <el-form-item label="门诊号" prop="outpatientNo">
          <el-input v-model="form.outpatientNo" placeholder="请输入门诊号" />
        </el-form-item>
        <el-form-item label="支付订单描述" prop="payOrderName">
          <el-input
            v-model="form.payOrderName"
            placeholder="请输入支付订单描述"
          />
        </el-form-item>
        <el-form-item label="支付单号" prop="payOrderNo">
          <el-input v-model="form.payOrderNo" placeholder="请输入支付单号" />
        </el-form-item>
        <el-form-item label="支付失效时间" prop="paymentExpirationTime">
          <el-date-picker
            clearable
            v-model="form.paymentExpirationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付失效时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-input v-model="form.paymentMethod" placeholder="请输入支付方式" />
        </el-form-item>
        <el-form-item label="支付方式名称" prop="paymentMethodName">
          <el-input
            v-model="form.paymentMethodName"
            placeholder="请输入支付方式名称"
          />
        </el-form-item>
        <el-form-item label="支付状态名称" prop="paymentStatusStr">
          <el-input
            v-model="form.paymentStatusStr"
            placeholder="请输入支付状态名称"
          />
        </el-form-item>
        <el-form-item label="支付时间" prop="paymentTime">
          <el-date-picker
            clearable
            v-model="form.paymentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收费次数" prop="numberCharges">
          <el-input v-model="form.numberCharges" placeholder="请输入收费次数" />
        </el-form-item>
        <el-form-item label="支付流水号" prop="paymentTransactionNo">
          <el-input
            v-model="form.paymentTransactionNo"
            placeholder="请输入支付流水号"
          />
        </el-form-item>
        <el-form-item label="支付类型" prop="paymentType">
          <el-select v-model="form.paymentType" placeholder="请选择支付类型">
            <el-option
              v-for="dict in dict.type.yy_pay_types"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="结算方式" prop="settlementMethod">
          <el-select
            v-model="form.settlementMethod"
            placeholder="请选择结算方式"
          >
            <el-option
              v-for="dict in dict.type.yy_settle_accounts"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结算方式名称" prop="settlementMethodName">
          <el-input
            v-model="form.settlementMethodName"
            placeholder="请输入结算方式名称"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.yy_pay_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款人id" prop="refundCheckerId">
          <el-input
            v-model="form.refundCheckerId"
            placeholder="请输入退款人id"
          />
        </el-form-item>
        <el-form-item label="退款人姓名" prop="refundCheckerName">
          <el-input
            v-model="form.refundCheckerName"
            placeholder="请输入退款人姓名"
          />
        </el-form-item>
        <el-form-item label="退款时间" prop="refundTime">
          <el-date-picker
            clearable
            v-model="form.refundTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退款时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款流水号" prop="refundTransactionNo">
          <el-input
            v-model="form.refundTransactionNo"
            placeholder="请输入退款流水号"
          />
        </el-form-item>
        <el-form-item label="医保退款结算id" prop="refundSetlId">
          <el-input
            v-model="form.refundSetlId"
            placeholder="请输入医保退款结算id"
          />
        </el-form-item>
        <el-form-item label="个人账户支付金额" prop="payCost">
          <el-input
            v-model="form.payCost"
            placeholder="请输入个人账户支付金额"
          />
        </el-form-item>
        <el-form-item label="统筹金额" prop="pubCost">
          <el-input v-model="form.pubCost" placeholder="请输入统筹金额" />
        </el-form-item>
        <el-form-item label="现金支付金额" prop="ownCost">
          <el-input v-model="form.ownCost" placeholder="请输入现金支付金额" />
        </el-form-item>
        <el-form-item label="医保支付订单号" prop="payOrdId">
          <el-input
            v-model="form.payOrdId"
            placeholder="请输入医保支付订单号"
          />
        </el-form-item>
        <el-form-item label="医保结算id" prop="setlId">
          <el-input v-model="form.setlId" placeholder="请输入医保结算id" />
        </el-form-item>
        <el-form-item label="医保登记id" prop="mdtrtId">
          <el-input v-model="form.mdtrtId" placeholder="请输入医保登记id" />
        </el-form-item>
        <el-form-item label="患者医保编号" prop="psnNo">
          <el-input v-model="form.psnNo" placeholder="请输入患者医保编号" />
        </el-form-item>
        <el-form-item label="医保结算参数" prop="hsPayInfo">
          <el-input
            v-model="form.hsPayInfo"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="其他支付参数" prop="otherPaymentParameters">
          <el-input
            v-model="form.otherPaymentParameters"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="参保地区划" prop="insuplcAdmdvs">
          <el-input
            v-model="form.insuplcAdmdvs"
            placeholder="请输入参保地区划"
          />
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-select v-model="form.source" placeholder="请选择来源">
            <el-option
              v-for="dict in dict.type.yy_source_types"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应付总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入应付总金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPayment,
  getPayment,
  delPayment,
  addPayment,
  updatePayment,
} from "@/api/dataCenter/payment";

export default {
  name: "Payment",
  dicts: [
    "yy_pay_status",
    "yy_source_types",
    "yy_pay_types",
    "yy_settle_accounts",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 云HIS-支付汇总表格数据
      paymentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientName: null,
        payOrderNo: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        //column.property == 'zeroTotalPrice'只要换上你想汇总列的prop属性值就可以实现某一行汇总
        if (
          column.property == "amount" ||
          column.property == "totalAmount" ||
          column.property == "actualPayment" ||
          column.property == "payCost" ||
          column.property == "pubCost" ||
          column.property == "ownCost"
        ) {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
          }
        }
      });
      return sums;
    },
    /** 查询云HIS-支付汇总列表 */
    getList() {
      this.loading = true;
      listPayment(this.queryParams).then((response) => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patientId: null,
        patientName: null,
        actualPayment: null,
        amount: null,
        checkerId: null,
        checkerName: null,
        outpatientNo: null,
        payOrderName: null,
        payOrderNo: null,
        paymentExpirationTime: null,
        paymentMethod: null,
        paymentMethodName: null,
        paymentStatus: null,
        paymentStatusStr: null,
        paymentTime: null,
        medicalInsuranceType: null,
        numberCharges: null,
        paymentTransactionNo: null,
        paymentType: null,
        remark: null,
        settlementMethod: null,
        settlementMethodName: null,
        status: null,
        refundCheckerId: null,
        refundCheckerName: null,
        refundTime: null,
        refundTransactionNo: null,
        refundSetlId: null,
        payCost: null,
        pubCost: null,
        ownCost: null,
        payOrdId: null,
        setlId: null,
        mdtrtId: null,
        psnNo: null,
        hsPayInfo: null,
        otherPaymentParameters: null,
        insuplcAdmdvs: null,
        insutype: null,
        source: null,
        tenantId: null,
        totalAmount: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.patientId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加云HIS-支付汇总";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const patientId = row.patientId || this.ids;
      getPayment(patientId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改云HIS-支付汇总";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //   this.form.display = this.form.display.join(",");
          if (this.form.id != null) {
            updatePayment(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayment(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该记录？")
        .then(function () {
          return delPayment(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dataCenter/payment/export",
        {
          ...this.queryParams,
        },
        `payment_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
